package org.mbds.wolf.android.wallet;

import java.util.Map;

import org.mbds.wolf.android.R;
import org.mbds.wolf.android.service.MyWolfHceService;
import org.mbds.wolf.seql.ISeqlCallBack;
import org.mbds.wolf.seql.ISeqlController;
import org.mbds.wolf.seql.SeqlController;
import org.mbds.wolf.seql.exceptions.APDUError;
import org.mbds.wolf.tools.BytesTool;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

/**
 * 
 * @author alesas
 *
 */
public class WolfWalletActivity extends Activity implements ISeqlCallBack {
	 private ISeqlController ctrl = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.wallet);

		init();
	}
	
	protected void init() {
		boolean ok = false;
		try {
			SeqlController.CONFIG os = SeqlController.CONFIG.ANDROID_HCE;
			ctrl =SeqlController.getController(getApplicationContext(), os, this, null, MyWolfHceService.class.getName());
			ok = ctrl.initService();
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (!ok) {
			Toast.makeText(this, "SE-QL controller could not be instantiated, application will finish!", Toast.LENGTH_SHORT).show();
			quitApp();
		} 
		Toast.makeText(this, "SE-QL controller successfully instantiated!", Toast.LENGTH_SHORT).show();
	}
	
	public void test(){
		ctrl.execute("insert into wolf_hce (firstname) values(Test0001)", 2);
		ctrl.execute("delete firstname from wolf_hce", 1);
		ctrl.execute("select firstname from wolf_hce", 3);
		ctrl.execute("insert into wolf_hce (firstname) values(Test0001)", 2);
		ctrl.execute("insert into wolf_hce (firstname) values(Test0002)", 4);
		ctrl.execute("select firstname from wolf_hce", 5);
		ctrl.execute("update from wolf_hce (firstname) values(TESTXXX) fetch 02", 6);
		ctrl.execute("select firstname from wolf_hce", 7);
		ctrl.execute("delete firstname from wolf_hce fetch 01", 8);
		ctrl.execute("select firstname from wolf_hce", 9);
	}
	
	public void quitApp() {
		System.exit(0);
	}

	public ISeqlController getSeqlController() {
		return ctrl;
	}
	

	@Override
	public void onNotConnected() {
		Toast.makeText(this, "Reader not connected error!", Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onConnected() {
		Toast.makeText(this, "Successfully connected to the Reader!", Toast.LENGTH_SHORT).show();
		test();
	}

	@Override
	public void onNoSecureElement() {
		Toast.makeText(this, "Secure Element not found error!", Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onNoReader() {
		Toast.makeText(this, "Reader not found error!", Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onNoApplet() {
		Toast.makeText(this, "Applet not found error!", Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onAPDUError(final APDUError error) {
		Toast.makeText(this, "APDU error!: "+error.getMessage(), Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onIOException(final String message) {
		Toast.makeText(this, "I/O exception error!", Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onPINRequired() {
		Toast.makeText(this, "PIN required!", Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onBadRequest(final String message) {
		Toast.makeText(this, "Bad request error! "+message, Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onResponse(final Map<String, Object> results, final int code) {
		String list = "";
		for (Map.Entry<String, Object> entry : results.entrySet()) {
			String str = (String) entry.getValue();
			try {
				byte[] b = BytesTool.hexStringToByteArray(str.replace(" ",  ""));
				str = new String(b);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			list+=str+",";
		}
		switch (code) {
		case 1: 
			Toast.makeText(this, "success cmd: 'delete firstname from wolf_hce", Toast.LENGTH_SHORT).show();
			break;
		case 2:
			Toast.makeText(this, "success cmd: 'insert into wolf_hce (firstname) values(Test0001)'", Toast.LENGTH_SHORT).show();
			break;
		case 3:
			Toast.makeText(this, "select firstname from wolf_hce: "+list, Toast.LENGTH_SHORT).show();
			break;
		case 4:
			Toast.makeText(this, "success cmd: 'insert into wolf_hce (firstname) values(Test0002)'", Toast.LENGTH_SHORT).show();
			break;
		case 5:
			Toast.makeText(this, "select firstname from wolf_hce: "+list, Toast.LENGTH_SHORT).show();
			break;
		case 6:
			Toast.makeText(this, "success cmd: 'update from wolf_hce (firstname) values(TESTTEST) fetch 02", Toast.LENGTH_SHORT).show();
			break;
		case 7:
			Toast.makeText(this, "select firstname from wolf_hce: "+list, Toast.LENGTH_SHORT).show();
			break;
		case 8:
			Toast.makeText(this, "success cmd: 'delete firstname from wolf_hce fetch 01'", Toast.LENGTH_SHORT).show();
			break;
		case 9:
			Toast.makeText(this, "select firstname from wolf_hce: "+list, Toast.LENGTH_SHORT).show();
			break;
		}
	}

	@Override
	public void onConfigError() {
		Toast.makeText(this, "Config error!", Toast.LENGTH_LONG).show();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		
		return super.onOptionsItemSelected(item);
	}

}
